package com.beirtipol.travel.activities;

import java.util.HashMap;
import java.util.Map;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.beirtipol.travel.R;
import com.beirtipol.travel.activities.dom.Line;
import com.beirtipol.travel.activities.dom.LineStatus;
import com.beirtipol.travel.sax.SAXFactory;
import com.beirtipol.travel.sax.StatusHandler;

public class StatusActivity2 extends ListActivity
{
	private ItemsAdapter		adapter;
	private StatusHandler		statusHandler;

	private Map<Line, Boolean>	expandedMap	= new HashMap<Line, Boolean>();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.status);
		statusHandler = new StatusHandler();
		new SAXFactory().parse(statusHandler);
		this.adapter = new ItemsAdapter(this, R.layout.status, Line.values());
		setListAdapter(this.adapter);
		resetExpandedMap();
	}

	private void resetExpandedMap()
	{
		for (Line line : Line.values())
		{
			expandedMap.put(line, false);
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		resetExpandedMap();
		Line line = Line.values()[position];
		Boolean currentState = expandedMap.get(line);
		resetExpandedMap();
		if (!currentState)
		{
			expandedMap.put(line, true);
		}
		adapter.notifyDataSetChanged();
	}

	private class ItemsAdapter extends ArrayAdapter<Line>
	{
		private Line[]	items;

		public ItemsAdapter(Context context, int textViewResourceId, Line[] items)
		{
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			Line thisItem = items[position];
			LineStatus status = statusHandler.getLineStatusMap().get(thisItem);

			// LayoutInflater inflater = getLayoutInflater();
			// TableLayout table = (TableLayout)
			// inflater.inflate(R.layout.statusrow, null);

			TableLayout table = new TableLayout(getContext());
			table.setStretchAllColumns(true);

			TableRow headerRow = new TableRow(getContext());
			{
				TextView lineView = new TextView(getContext());
				lineView.setText(thisItem.getName());
				headerRow.addView(lineView);

				TextView descView = new TextView(getContext());
				descView.setText(status.getDescription());
				descView.setGravity(Gravity.RIGHT);
				headerRow.addView(descView);
			}
			table.addView(headerRow);

			TableRow detailsRow = new TableRow(getContext());
			{
				TextView detailsView = new TextView(getContext());
				detailsView.setText(status.getStatusDetails());
				detailsRow.addView(detailsView, new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
				detailsRow.setVisibility(expandedMap.get(thisItem) ? View.VISIBLE : View.GONE);
			}
			table.addView(detailsRow);

			return table;

			// TableLayout tableLayout = new TableLayout(getContext());
			// TableRow headerRow = new TableRow(getContext());
			// tableLayout.addView(headerRow);
			//
			// mTitle = new TextView(getContext());
			// mTitle.setText(thisItem.getName());
			// headerRow.addView(mTitle, new
			// LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
			// LayoutParams.WRAP_CONTENT));
			//
			// mStatus = new TextView(getContext());
			// mStatus.setText(status.getDescription());
			// headerRow.addView(mStatus, new
			// LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
			// LayoutParams.WRAP_CONTENT));
			//
			// detailsRow = new TableRow(getContext());
			// tableLayout.addView(detailsRow);
			//
			// mDetails = new TextView(getContext());
			// mDetails.setText(status.getStatusDetails());
			// detailsRow.addView(mDetails, new
			// LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
			// LayoutParams.WRAP_CONTENT));
			//
			// detailsRow.setVisibility(expandedMap.get(thisItem) ? View.VISIBLE
			// : View.GONE);
			// return tableLayout;

			// TextView tv = new TextView(getContext());
			//
			// tv.setText(thisItem.getName() + "-" + status.getDescription());
			// tv.setTextSize(16.0f);
			// return tv;
		}
	}
}
